package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.pojo.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 仲夏夜
 * @since 2022-09-16
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
@Slf4j
public class EduCourseController {
    @Resource
    private EduCourseService courseService;

    @GetMapping
    public R getCourseList() throws Exception {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo
     * @return
     * @throws Exception
     */
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws Exception {
        log.info("开始添加课程基本信息------------------------------------");
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);

        log.info("结束添加课程基本信息------------------------------------");
        return R.ok().data("courseId", id);
    }

    /**
     * 根据课程id查询课程基本信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) throws Exception {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    /**
     * 根据课程id修改课程基本信息
     *
     * @param courseInfoVo
     * @return
     */
    @GetMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws Exception {
        courseService.updateCourseInfoById(courseInfoVo);
        return R.ok();
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) throws Exception {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    /**
     * 课程最终发布  修改课程状态
     *
     * @param id
     * @return
     */
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) throws Exception {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 删除课程
     *
     * @param courseId
     * @return
     */
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) throws Exception {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}